package nobnak.study.gl.projection;

import android.opengl.Matrix;

public class TriangleUtil {
	public static float det(float[] a, float[] b) {
		return a[0] * b[1] - a[1] * b[0];
	}
	public static void bound(float[] boundX, float[] boundY, float[] vertices) {
		boundX[0] = Float.MAX_VALUE;
		boundX[1] = Float.MIN_VALUE;
		boundY[0] = Float.MAX_VALUE;
		boundY[1] = Float.MIN_VALUE;
		
		for (int i = 0; i < vertices.length; i+= 2) {
			float x = vertices[i], y = vertices[i+1];
			if (x < boundX[0])
				boundX[0] = x;
			if (boundX[1] < x)
				boundX[1] = x;
			if (y < boundY[0])
				boundY[0] = y;
			if (boundY[1] < y)
				boundY[1] = y;
		}
	}
	public static int blend(float dx, float dy, int c00, int c01, int c10, int c11) {
		return (int) (dy * (dx * c00 + (1 - dx) * c10) + (1 - dy) * (dx * c01 + (1 - dx) * c11));
	}
	public static float[] makeRasterMatrix(int width, int height, float near, float far, float fovy, float[] eye, float[] lookAt) {
		float[] viewMatrix = new float[Constant.MATRIX_4_BY_4];
		Matrix.setLookAtM(viewMatrix, 0, eye[0], eye[1], eye[2], lookAt[0], lookAt[1], lookAt[2], 0, 1, 0);
		float[] projectionMatrix = new float[Constant.MATRIX_4_BY_4];
		float aspect = (float) width / height;
		MatrixMath.makePerspectiveMatrix(projectionMatrix, 0, 45, aspect, near, far);
		
		float[] viewportMatrix = new float[Constant.MATRIX_4_BY_4];
		MatrixMath.makeViewportMatrix(viewportMatrix, 0, width, height);
		
		float[] vpMatrix = new float[Constant.MATRIX_4_BY_4];
		MatrixMath.multiplyMatrix(vpMatrix, 0, projectionMatrix, 0, viewMatrix, 0);
		final float[] vpvMatrix = new float[Constant.MATRIX_4_BY_4];
		MatrixMath.multiplyMatrix(vpvMatrix, 0, viewportMatrix, 0, vpMatrix, 0);
		return vpvMatrix;
	}
}
